package com.rainng.coursesystem.util;


import com.spire.doc.*;
import com.spire.doc.documents.*;

import com.spire.doc.fields.TextRange;

public class docxUtil {

    public static final String BOOKMARK_START_TAG = "w:bookmarkStart";
    /**
     * word书签结束部分
     */
    public static final String BOOKMARK_END_TAG = "w:bookmarkEnd";


    public static void main(String[] args)   {


        //加载包含书签的Word文档

        Document doc2 = new Document();

        doc2.loadFromFile("D://docx//data//1G1BL52P7TR115520_272_营运货车+05-1营运货车+操纵稳定性检验记录+HWJC-549-1-20211009.docx");

//        bookmarkTest(doc2);
        //获取书签集合
        //获取书签
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc2);
        bookmarksNavigator.moveToBookmark("test1");
        //获取书签文本
        TextBodyPart textBodyPart = bookmarksNavigator.getBookmarkContent();
        //创建String变量
        String text = "";
        //遍历书签内容的项目
        for (Object item : textBodyPart.getBodyItems()) {

            //判断项目是否为段落
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //遍历段落中的子对象
                for (Object childObj : paragraph.getChildObjects()) {

                    //判断子对象是否为TextRange
                    if (childObj instanceof TextRange) {

                        //获取TextRange中的文本
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                        String CC="";
                    }
                }
            }
        }
        String vv="" ;


    }

    //通过书签获取书签参数
    public static String getValueByDocxTag(Document doc2,String bookMarkName){
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc2);
        bookmarksNavigator.moveToBookmark(bookMarkName);
        String text = "";
        try {
            //获取书签文本
            TextBodyPart textBodyPart = bookmarksNavigator.getBookmarkContent();
            //创建String变量
            //遍历书签内容的项目
            for (Object item : textBodyPart.getBodyItems()) {

                //判断项目是否为段落
                if (item instanceof Paragraph) {
                    Paragraph paragraph = (Paragraph) item;

                    //遍历段落中的子对象
                    for (Object childObj : paragraph.getChildObjects()) {

                        //判断子对象是否为TextRange
                        if (childObj instanceof TextRange) {

                            //获取TextRange中的文本
                            TextRange textRange = (TextRange) childObj;
                            text = text + textRange.getText();
                            String CC = "";
                        }
                    }
                }
            }
        }
        catch(Exception ex){

        }
        return text;

    }


    private static void bookmarkTest(Document document) {
        //定位到书签
        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
        //移除
        bookmarkNavigator.moveToBookmark("test2");
        String title = "□";
        //使用文本替换原书签的内容, false表示不保留原来的格式
        bookmarkNavigator.replaceBookmarkContent(title, false);

        document.saveToFile("D://新文档路径.docx", FileFormat.Docx_2013);
        document.dispose();
    }
}




